import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'  # 这句放在pygame导入之前
from game import Game
import pygame 
import sys
import time 
from constants import * 

def main():
    game = Game()
    pygame.init() 
    game.screen = pygame.display.set_mode((game.display_width,game.display_height))
    pygame.display.set_caption("Greedy Snake Game") 
    game.font = pygame.font.Font(None,36) 
    
    game_state = "welcome"
    # 两个功能按键
    start_button = game.font.render("START" , True, BLACK)
    retry_button = game.font.render("RETRY" , True , BLACK)
    
    update_interval = .15
    start_time = time.time() 
    action = -1  # 设置成非法值
    clock = pygame.time.Clock()
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
                
            if game_state == 'running'  :
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP :
                        action = UP 
                    elif event.key == pygame.K_DOWN :
                        action = DOWN
                    elif event.key == pygame.K_LEFT :
                        action = LEFT 
                    elif event.key == pygame.K_RIGHT :
                        action = RIGHT 
            
            if game_state == 'welcome' and event.type == pygame.MOUSEBUTTONDOWN :
                if game.is_mouse_on_button(start_button) :
                    # 倒计时3秒
                    for i in range(3,0,-1) :
                        game.screen.fill((0,0,0))
                        game.draw_countdown(i)
                        if not game.silent_mode : 
                            game.count_down.play()
                        pygame.time.wait(1000) 
                    action = DOWN
                    game_state = 'running'
                    
            if game_state == 'game_over' and event.type == pygame.MOUSEBUTTONDOWN :
                if game.is_mouse_on_button(retry_button) :
                    for i in range(3,0,-1) :
                        game.screen.fill(BLACK)
                        game.draw_countdown(i) 
                        if not game.silent_mode : 
                            game.count_down.play()
                        pygame.time.wait(1000)
                    game.reset()
                    action = DOWN
                    game_state = 'running' 
                    
        # end for loop 
        if game_state == 'welcome' :
            game.draw_welcome_screen()
        if game_state =='game_over' :
            game.draw_game_over_screen() 
        if game_state == 'running' :
            if time.time() - start_time >= update_interval :
                die , _ = game.step(action)
                game.render() 
                start_time = time.time() # 更新时间
                if die :
                    game_state = 'game_over' 
        clock.tick(60)
    # end while loop 

if __name__ == "__main__":
    main()
